<template>
  <div>
    <el-upload
      ref="upload"
      :action="url"
      :limit="1"
      :on-error="onError"
      :on-preview="handlePreview"
      :on-success="handleAvatarSuccess"
      :file-list="fileList"
      :auto-upload="false"
      class="upload-demo">
      <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
      <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">
        上传到服务器
      </el-button>
    </el-upload>

    <!--查看pdf-->
    <el-dialog
      :visible.sync="showModel"
      width="30%"
    >
      <img v-if="isImg" :src="previewUrl" style="width:100%;" alt="">
      <iframe v-else :src="previewUrl" style="width:100%;min-height:60vh" frameborder="0"/>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'UploadFiles',
  props: {
    url: {
      type: String,
      default: 'https://tuitui.wxtt.cn/storage/image'
    },
    fileList: {
      type: Array,
      default: []
    }
  },
  data() {
    return {
      showModel: false,
      isImg: false,
      previewUrl: ''
    }
  },
  methods: {
    submitUpload() {
      this.$refs.upload.submit()
    },
    handleAvatarSuccess(response, file, fileList) {
      console.log(response, file, fileList)
      this.$emit('on-success', response, file, fileList)
    },
    handlePreview(file) {
      this.isImg = !/.pdf$/.test(file.name)
      this.showModel = true
      this.previewUrl = file.url
    },
    onError(event, file, fileList) {
      console.log(event, file, fileList)
      this.$message('上传失败,请重试')
    }

  }
}
</script>

<style scoped>

</style>
